/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RobotFrame.java
 *
 * Created on Dec 8, 2010, 9:21:30 PM
 */

package robot;

import java.io.PrintStream;
import RobotPkg.*;
import com.ibm.rational.rhapsody.oxf.*;
import java.util.ListIterator;

/**
 *
 * @author janburkot
 */
public class RobotFrame extends javax.swing.JFrame {
    
    protected static RobotTester p_RobotTester = null;
    protected static int sensors[] = new int[6];
    protected static int onoff=0;
    protected static RobotController robot = null;
    
    
    /** Creates new form RobotFrame */
    public RobotFrame() {
        initComponents();
        System.setOut(new PrintStream(new TextAreaOutputStream(jTextArea1)));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jButton7 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 100, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jButton7.setText("ON/OFF");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton7);

        jButton1.setText("1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton1);

        jButton2.setText("2");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton2);

        jButton3.setText("3");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton3);

        jButton4.setText("4");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton4);

        jButton5.setText("5");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton5);

        jButton6.setText("6");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton6);

        jPanel1.add(jPanel3, java.awt.BorderLayout.LINE_START);

        jSplitPane1.setTopComponent(jPanel1);

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setRows(5);
        jTextArea1.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
        jTextArea1.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextArea1.setEnabled(false);
        jScrollPane1.setViewportView(jTextArea1);

        jSplitPane1.setRightComponent(jScrollPane1);

        getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        // wyslac sygnal do 1 czujnika
        System.out.println("wyslac sygnal do 1 czujnika");
        RobotFrame.sensors[0] = (RobotFrame.sensors[0] == 0)? 1: 0;
        ListIterator<Sensor> itr = RobotFrame.robot.getItsSensor();
        for(int i=1; i<1; i++)
            itr.next();
        
        if(RobotFrame.sensors[0]==1){
            itr.next().gen(new lineSeen());
        }
        else{
            itr.next().gen(new lineNotSeen());
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        System.out.println("wyslac sygnal do 2 czujnika");
        RobotFrame.sensors[1] = (RobotFrame.sensors[1] == 0)? 1: 0;
        ListIterator<Sensor> itr = RobotFrame.robot.getItsSensor();
        for(int i=1; i<2; i++)
            itr.next();
        
        if(RobotFrame.sensors[1]==1){
            itr.next().gen(new lineSeen());
        }
        else{
            itr.next().gen(new lineNotSeen());
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
        //System.out.println("ON/OFF");
        RobotFrame.robot.gen(new evOnOff());
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        System.out.println("wyslac sygnal do 3 czujnika");
        RobotFrame.sensors[2] = (RobotFrame.sensors[2] == 0)? 1: 0;
        ListIterator<Sensor> itr = RobotFrame.robot.getItsSensor();
        for(int i=1; i<3; i++)
            RobotFrame.robot.getItsSensor().next();
        
        if(RobotFrame.sensors[2]==1){
            itr.next().gen(new lineSeen());
        }
        else{
            itr.next().gen(new lineNotSeen());
        }
        
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        System.out.println("wyslac sygnal do 4 czujnika");
        RobotFrame.sensors[3] = (RobotFrame.sensors[3] == 0)? 1: 0;
        ListIterator<Sensor> itr = RobotFrame.robot.getItsSensor();
        for(int i=1; i<4; i++)
            itr.next();
        
        if(RobotFrame.sensors[3]==1){
            itr.next().gen(new lineSeen());
        }
        else{
            itr.next().gen(new lineNotSeen());
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        //System.out.println("wyslac sygnal do 5 czujnika");
        RobotFrame.sensors[4] = (RobotFrame.sensors[4] == 0)? 1: 0;
        ListIterator<Sensor> itr = RobotFrame.robot.getItsSensor();
        for(int i=1; i<5; i++)
            itr.next();
        
        if(RobotFrame.sensors[4]==1){
            itr.next().gen(new lineSeen());
        }
        else{
            itr.next().gen(new lineNotSeen());
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
        //System.out.println("wyslac sygnal do 6 czujnika");
        RobotFrame.sensors[5] = (RobotFrame.sensors[5] == 0)? 1: 0;
        ListIterator<Sensor> itr = RobotFrame.robot.getItsSensor();
        for(int i=1; i<6; i++)
            itr.next();
        
        if(RobotFrame.sensors[5]==1){
            itr.next().gen(new lineSeen());
        }
        else{
            itr.next().gen(new lineNotSeen());
        }
    }//GEN-LAST:event_jButton6ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        
        RiJOXF.Init(null, 0, 0, true, args);
        robot = new RobotController(RiJMainThread.instance());
        robot.startBehavior();
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RobotFrame().setVisible(true);
            }
        });
        RiJOXF.Start();
        robot=null;
//        RiJOXF.Init(null, 0, 0, true, args);
//        //MainTest initializer_Test = new MainTest();
//        //p_RobotTester = new RobotTester(RiJMainThread.instance());
//        //p_RobotTester.startBehavior();
//        //#[ configuration Test::DefaultConfig 
//        //#]
//        //RiJOXF.Start();
//        //p_RobotTester=null;
//        
//        robot = new RobotController(RiJMainThread.instance());
//        robot.startBehavior();
//        
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new RobotFrame().setVisible(true);
//            }
//        });
//        RiJOXF.Start();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables

}
